using System.Collections;
using System.ComponentModel;
using Send2Sharepoint.Common;

namespace Send2Sharepoint.Outlook
{
    public class UploadOutlookSelectedItems : WorkItem
    {
        public ArrayList SelectedItems { get; private set; }

        #region IWorkItem Members

        public UploadOutlookSelectedItems(ArrayList selectedItems)
        {
            SelectedItems = selectedItems;
        }

        internal override void Upload(BackgroundWorker worker, DoWorkEventArgs e)
        {
            var totalItems = SelectedItems.Count;
            var count = 1;
            int lastPercentDone = 0;
            GenericOutlookItem genericOutlookitem;

            foreach (var oItem in SelectedItems)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                genericOutlookitem = new GenericOutlookItem(oItem);
                genericOutlookitem.Upload(IndexInformation);
                genericOutlookitem.Dispose();

                int percentDone = (count / totalItems) * 100;
                if (percentDone > lastPercentDone)
                {
                    worker.ReportProgress(percentDone);
                    lastPercentDone = percentDone;
                }
                count++;
            }
        }
        #endregion
    }
}
